/**
 *
 */
package com.delta.rafg.core.log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.delta.rafg.core.AObjectHelperImpl;
import com.delta.rafg.coredata.AObject;

/**
 * @author D9785692
 *
 */
public class LogDataUtil extends AObject {

	private static final long serialVersionUID = 6125911547151940815L;

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
	        if (getaObj() == null) {
	            setaObj(new AObjectHelperImpl());
	        }

	    LogData aLd = new LogData("HELLO",new Exception(),null, null, (Object[])null);
	    System.out.println(aLd.toXML());
		LogDataUtil aLDU = LogDataUtil.get();
		System.out.println(aLDU.fromByteArray(aLDU.toByteArray(aLd)).toXML());
	}


    /**
     * Converts the byte array containing any AObject stream into an AObject.
     *
     * @param pBytes
     * @return
     */
    public AObject fromByteArray( byte[] pBytes ) {
		try {
	         byte [] data = pBytes;
	         ObjectInputStream ois;
	         ois = new ObjectInputStream( new ByteArrayInputStream(  data ) );
	         Object o  = ois.readObject();
	         ois.close();
	         return (AObject)o;
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
     }

    /**
     * Converts any AObject into a byte array stream.
     *
     * @param pAObject
     * @return
     */
    public byte[] toByteArray( AObject pAObject ){
		try {
	         ByteArrayOutputStream baos = new ByteArrayOutputStream();
	         ObjectOutputStream oos;
	         oos = new ObjectOutputStream( baos );
	         oos.writeObject( pAObject );
	         oos.close();
	         return baos.toByteArray();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return null;
     }

    public static LogDataUtil get(){
    	return new LogDataUtil();
    }
}

